<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

<!-- Jquery table Grid -->
<script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">


<script>

$(document).ready(function() {
    $('#example').DataTable( {
    	    "bLengthChange": false,
			"iDisplayLength": 50,
			"fnDrawCallback": function(oSettings) {
		        if ($('#example tr').length < 50) {
		            $('.dataTables_paginate').hide();
		        }
		    }
    });
} );

</script>

<style>

table.dataTable thead th, table.dataTable thead td{
  padding: 9px;
}

</style>
<?php

if( ! ini_get('date.timezone') )
{
    date_default_timezone_set('GMT');
}

function get_curl($curl){
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_USERPWD, 'eportal:wyua2612');
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);


  $response = curl_exec($curl);
  $resultStatus = curl_getinfo($curl);
  $fp = fopen('statistics.xml', 'w');
  chmod("statistics.xml",0777);
  $xml = simplexml_load_string($response);
  return $xml;
}

/*
 * Cumulative Stat
 */

$this_month = mktime(0, 0, 0, date('m'), 1, date('Y'));

$date_val_st = date("d/m/Y", strtotime("-2 month", $this_month));
$date_val_end = date("t/m/Y", strtotime("-2 month", $this_month));

//print_r(array($date_val_st, $date_val_end));

$cumulative_curl = curl_init('http://164.100.167.34:8080/edistrict_tn_deity/getDistrictWiseCount/forGnfromDatetoDate/' .$date_val_st. '/' . $date_val_end );

$revenue_curl = curl_init('http://164.100.167.34:8080/edistrict_tn_deity/getDistrictWiseRevenueCount/forGnfromDatetoDate/' .$date_val_st. '/' . $date_val_end );

$social_curl = curl_init('http://164.100.167.34:8080/edistrict_tn_deity/getDistrictWiseSocialWelfareCount/forGnfromDatetoDate/' .$date_val_st. '/' . $date_val_end );

$cumulative = get_curl($cumulative_curl);
$revenue = get_curl($revenue_curl);
$social = get_curl($social_curl);



?>
<h3 align="center"> District wise e-District Transaction Count for the month of <?php echo date("F Y", strtotime("-2 month", $this_month)); ?> - Tamil Nadu</h3>
<table width="80%" cellspacing="0" cellpadding="0" align="center" border="1" id="example" class="display">
<thead>
  <tr>
    <th rowspan="2">S.No.</th>
    <th rowspan="2">Districts</th>
    <th colspan="2">Revenue</th>
    <th colspan="2">Social Welfare</th>
    <th colspan="2">Total</th>
  </tr>
  <tr >
    <th>Count</th>
    <th>Rank</th>
    <th>Count</th>
    <th>Rank</th>
    <th>Count</th>
    <th>Rank</th>
  </tr>
</thead>
<tbody>
<?php
if(count($cumulative->response)){

    $revenue_tot_count = 0;
    $revenue_tot_rank = 0;
    $social_tot_count = 0;
    $social_tot_rank = 0;
    $cumulative_tot_count = 0;
    $cumulative_tot_rank = 0;

  for($a=0; $a<count($cumulative->response); $a++){
    for($b=0; $b<count($social->response); $b++){
       if(trim($cumulative->response[$a]->DistrictName) == trim($social->response[$b]->DistrictName)) {
          $social_count = $social->response[$b]->Count;
          $social_rank = $social->response[$b]->Rank;
          break;
       }
       else {
        $social_count = 0;
        $social_rank = 0;
      }

    }

    $revenue_tot_count += $revenue->response[$a]->Count;
    $revenue_tot_rank += $revenue->response[$a]->Rank;
    $social_tot_count += $social_count;
    $social_tot_rank += $social_rank;
    $cumulative_tot_count += $cumulative->response[$a]->Count;
    $cumulative_tot_rank += $cumulative->response[$a]->Rank;


?>

  <tr>
    <td><?php echo $cumulative->response[$a]->slno;?></td>
    <td><?php echo $cumulative->response[$a]->DistrictName;?></td>
    <td><?php echo $revenue->response[$a]->Count;?></td>
    <td><?php echo $revenue->response[$a]->Rank;?></td>
    <td><?php echo $social_count;?></td>
    <td><?php echo $social_rank;?></td>
    <td><?php echo $cumulative->response[$a]->Count;?></td>
    <td><?php echo $cumulative->response[$a]->Rank;?></td>
  </tr>
<?php } ?>
<!--
  <tr>
    <td colspan="2">Total</td>
    <td><?php echo $revenue_tot_count; ?></td>
    <td><?php echo $revenue_tot_rank; ?></td>
    <td><?php echo $social_tot_count; ?></td>
    <td><?php echo $social_tot_rank; ?></td>
    <td><?php echo $cumulative_tot_count; ?></td>
    <td><?php echo $cumulative_tot_rank; ?></td>
  </tr>
 -->
<?php }?>
</tbody>
</table>